package com.example.routeplansystem.controller;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.routeplansystem.anotation.AllowAccess;
import com.example.routeplansystem.anotation.UserPermissionConf;
import com.example.routeplansystem.constant.ResponseStatus;
import com.example.routeplansystem.entity.dto.ResponseEntity;
import com.example.routeplansystem.entity.po.UserMessage;
import com.example.routeplansystem.entity.vo.UserInfo;
import com.example.routeplansystem.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "提供用户登录和注册接口", tags = "用户管理")
@RestController
@RequestMapping("userSystem")
@Validated
@Slf4j
@UserPermissionConf
public class UserController {

    @Autowired
    private UserService userService;

    //用户登录接口
    @PostMapping("/session/user")
    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userInfo", value = "用户信息", dataTypeClass = UserInfo.class, required = true),
    })
    @AllowAccess
    public ResponseEntity<UserMessage> userLogin(@RequestBody UserInfo userInfo, HttpServletRequest request,
            HttpServletResponse response) {
        //验证用户信息：
        return userService.userLogin(userInfo, request, response);
    }

    //登录验证码
    @GetMapping("/verifyCode")
    @ApiOperation("用户登录验证码")
    @AllowAccess
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        userService.generateVerifyCode(request, response);
    }

    //用户注册：
    @PostMapping(value = "/user")
    @ApiOperation("用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userInfo", value = "用户信息", dataTypeClass = UserInfo.class, required = true),
    })
    @AllowAccess
    public ResponseEntity<UserMessage> userRegister(@RequestBody UserInfo userInfo, HttpServletRequest request,
            HttpSession session) {
        //注册用户：
        return userService.userRegister(userInfo, request, session);
    }

    //邮箱验证码生成发送：
    @GetMapping(value = "/user/eMailCode")
    @ApiOperation("邮箱发送注册验证码")
    @ApiImplicitParam(name = "email", value = "邮箱", dataTypeClass = String.class, required = true)
    @AllowAccess
    public ResponseEntity<String> verifyMail(@RequestParam("email") String email, HttpServletRequest request,
            HttpServletResponse response) {
        //发送邮箱验证码：
        return userService.sendVerifyMail(email, request, response);
    }

    //用户注销：
    @DeleteMapping(value = "/session/user")
    @ApiOperation("用户退出登录")
    public ResponseEntity<String> userLogOut(HttpServletRequest request) {
        return userService.loginOut(request);
    }

    //用户自动登录：
    @GetMapping("/autologin")
    @ApiOperation("自动登录接口, 需要开启Cookie")
    @AllowAccess
    public ResponseEntity<String> autoLogin(HttpServletRequest request) {
        boolean flag = userService.autoLogin(request);
        if (flag) {
            return new ResponseEntity<>(ResponseStatus.SUCCESS_RESPONSE, "自动登录成功！！！", null);
        } else {
            return new ResponseEntity<>(ResponseStatus.FAILURE_RESPONSE, "自动登录失败！！！", null);
        }
    }

    // 管理员操作
    @PostMapping("/manage/operation")
    @ApiOperation("管理员操作接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userMessage", value = "用户信息", dataType = "om.example.routeplansystem.entity.po"
                    + ".UserMessage", required = true),
            @ApiImplicitParam(name = "operationType", value = "操作类型，1：新增用户，2：删除用户，3：修改用户，4：查询用户信息", dataType = "int",
                    required = true)
    })
    public ResponseEntity manage(@RequestBody UserMessage userMessage,
            @RequestParam("operationType") Integer operationType) {
        return userService.manageUser(userMessage, operationType);
    }
}
